﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using B2B.Data;
using System.Windows.Threading;
using B2B.Web;

namespace B2B.Views
{
    public partial class Home : Page
    {

        List<StokUretici> Ureticiler = new List<StokUretici>();
        List<StokMarka> Markalar = new List<StokMarka>();
        List<StokKategori> Kategoriler = new List<StokKategori>();
        List<StokAnaGrup> AnaGruplar = new List<StokAnaGrup>();
        List<StokAltgrup> AltGruplar = new List<StokAltgrup>();
        b2bDomainContext ctx = new b2bDomainContext();

        public Home()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            Albums album = new Albums();
            album.PhotosCollection.Add(new Photo() { Name = "Resim1", ImageUrl = "/B2B;component/images/1.png" });
            album.PhotosCollection.Add(new Photo() { Name = "Resim2", ImageUrl = "/B2B;component/images/2.png" });
            album.PhotosCollection.Add(new Photo() { Name = "Resim3", ImageUrl = "/B2B;component/images/3.png" });
            album.PhotosCollection.Add(new Photo() { Name = "Resim4", ImageUrl = "/B2B;component/images/4.png" });

            PhotosListBox.DataContext = album;
            PhotosListBox.SelectedIndex = 0;

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 5000);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();


        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            FillListbox();

            lstUreticiler.SelectionChanged += (ss, ee) =>
            {
                if (lstUreticiler.SelectedItem != null)
                {
                    lstMarkalar.ItemsSource = from m in Markalar where m.mrk_kod.StartsWith(((StokUretici)(lstUreticiler.SelectedItem)).urt_kod + ".") select m;
                    lstMarkalar.DisplayMemberPath = "mrk_ismi";
                }
            };

            lstKategori.SelectionChanged += (ss, ee) =>
            {
                if (lstKategori.SelectedItem != null)
                {
                    lstAnaGrup.ItemsSource = from a in AnaGruplar where a.san_kod.StartsWith(((StokKategori)(lstKategori.SelectedItem)).ktg_kod + ".") select a;
                    lstAnaGrup.DisplayMemberPath = "san_isim";
                    lstAltGrup.ItemsSource = null;
                }
            };

            lstAnaGrup.SelectionChanged += (ss, ee) =>
            {
                if (lstAnaGrup.SelectedItem != null)
                {
                    lstAltGrup.ItemsSource = from a in AltGruplar where a.sta_ana_grup_kod == ((StokAnaGrup)(lstAnaGrup.SelectedItem)).san_kod select a;
                    lstAltGrup.DisplayMemberPath = "sta_isim";
                }
            };
        }

        private void FillListbox()
        {
            lstUreticiler.ItemsSource = ctx.StokUreticis;
            lstUreticiler.DisplayMemberPath = "urt_ismi";
            ctx.Load(ctx.GetStokUreticileriQuery()).Completed += (s, e) =>
            {
                Ureticiler = ctx.StokUreticis.ToList();
            };


            lstKategori.ItemsSource = ctx.StokKategoris;
            lstKategori.DisplayMemberPath = "ktg_isim";
            ctx.Load(ctx.GetStokKategorileriQuery()).Completed += (s, e) =>
            {
                Kategoriler = ctx.StokKategoris.ToList();
            };

            lstMarkalar.ItemsSource = ctx.StokMarkas; ;
            lstMarkalar.DisplayMemberPath = "mrk_ismi";
            ctx.Load(ctx.GetStokMarkalariQuery()).Completed += (s, e) =>
            {
                Markalar = ctx.StokMarkas.ToList();
            };

            lstAnaGrup.ItemsSource = ctx.StokAnaGrups;
            lstAnaGrup.DisplayMemberPath = "san_isim";
            ctx.Load(ctx.GetStokAnaGruplariQuery()).Completed += (s, e) =>
            {
                AnaGruplar = ctx.StokAnaGrups.ToList();
            };

            lstAltGrup.ItemsSource = ctx.StokAltgrups;
            lstAltGrup.DisplayMemberPath = "sta_isim";
            ctx.Load(ctx.GetStokAltgruplariQuery()).Completed += (s, e) =>
            {
                AltGruplar = ctx.StokAltgrups.ToList();
            };

        }

        

        void timer_Tick(object sender, EventArgs e)
        {
            if ((PhotosListBox.SelectedIndex + 1) >= PhotosListBox.Items.Count)
            {
                PhotosListBox.SelectedIndex = 0;
            }
            else
                PhotosListBox.SelectedIndex += 1;
        }


        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }
}